package com.rongshu.view.penpal.present;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.penpal.bean.PenpalDetailsResponse;
import com.rongshu.view.penpal.view.PenpalHomeView;
import com.rongshu.widget.BanyanDialog;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/23.
 * description：
 */
public class PenpalHomePresenter extends BasePresenter<PenpalHomeView> {
    private BanyanDialog dialog;

    public PenpalHomePresenter(Context context) {
        super(context);
    }

    public void getPenpalDetails(int penpalwill_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        RetorfitService.getPenpalDetailsById(UserInfoUtils.getUserId(), penpalwill_id,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<PenpalDetailsResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PenpalDetailsResponse object) {
                getView().callBackPenpalMsg(object);
            }
        });
    }

    public void requestAddPenpal(int penpalwill_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        RetorfitService.requestAddPenpal(UserInfoUtils.getUserId(), penpalwill_id,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                if (object.getErrCode() == 0) {
                    getView().callBackJoinRequest();
                } else {
                    ToastUtils.show(object.getErrMsg());
                }
            }
        });
    }

    public void confirmExitPenpal(final int penpalwill_id) {
        BanyanDialog.Builder builder = new BanyanDialog.Builder(mContext);
        builder.setTitle("确定要退出笔友会吗？");
        builder.setLeft("取消");
        builder.setRight("确定");
        builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
            @Override
            public void leftClickListener() {
                dialog.dismiss();
            }

            @Override
            public void rightClickListener() {
                exitPenpal(penpalwill_id);
            }
        });
        dialog = builder.create();
        dialog.show();
    }

    public void exitPenpal(int penpalwill_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        RetorfitService.exitPenpal(UserInfoUtils.getUserId(), penpalwill_id,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                if (object.getErrCode() == 0) {
                    getView().callBackExitPenpal();
                }
            }
        });
    }
}
